<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">产品名称</label>
        <el-input v-model="query.prodName" clearable placeholder="产品名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">产品类型</label>
        <el-input v-model="query.prodType" clearable placeholder="产品类型" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">产品批次号</label>
        <el-input v-model="query.prodBatch" clearable placeholder="产品批次号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">产品备注</label>
        <el-input v-model="query.prodMemo" clearable placeholder="产品备注" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="产品名称" prop="prodName">
            <el-input v-model="form.prodName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="产品类型" prop="prodType">
            <el-input v-model="form.prodType" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="产品批次号">
            <el-input v-model="form.prodBatch" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="单位">
            <el-input v-model="form.prodUnit" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="单价">
            <el-input v-model="form.prodPrice" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="产品备注">
            <el-input v-model="form.prodMemo" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="prodId" label="产品编号" />
        <el-table-column prop="prodName" label="产品名称" />
        <el-table-column prop="prodType" label="产品类型" />
        <el-table-column prop="prodBatch" label="产品批次号" />
        <el-table-column prop="prodUnit" label="单位" />
        <el-table-column prop="prodPrice" label="单价" />
        <el-table-column prop="prodMemo" label="产品备注" />
        <el-table-column v-if="checkPer(['admin','product:edit','product:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <div>
      <div id="chart" style="width: 38rem; height: 38rem;display:inline-block;" />
      <div id="main1" style="width: 38rem; height: 38rem;display:inline-block;" />
      <div id="main2" style="width: 30rem; height: 30rem;display:inline-block;" />
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import crudProduct from '@/api/product'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { prodId: null, prodName: null, prodType: null, prodBatch: null, prodUnit: null, prodPrice: null, prodMemo: null }
export default {
  name: 'Product',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '产品信息', url: 'api/product', idField: 'prodId', sort: 'prodId,desc', crudMethod: { ...crudProduct }})
  },
  data() {
    return {
       list: [],
      permission: {
        add: ['admin', 'product:add'],
        edit: ['admin', 'product:edit'],
        del: ['admin', 'product:del']
      },
      rules: {
        prodName: [
          { required: true, message: '产品名称不能为空', trigger: 'blur' }
        ],
        prodType: [
          { required: true, message: '产品类型不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'prodName', display_name: '产品名称' },
        { key: 'prodType', display_name: '产品类型' },
        { key: 'prodBatch', display_name: '产品批次号' },
        { key: 'prodMemo', display_name: '产品备注' }
      ]
    }
  },
  mounted() {
    setTimeout(() => {
      const data1 = []
      const data2 = []
      this.list.forEach(e => {
        data1.push(e.prodName)
        data2.push(e.prodPrice)
      })
      const data3 = this.list.map((item, index) => {
        return Object.assign({}, { 'name': item.prodName, 'value': item.prodPrice })
      })
      console.log(data1)
      this.chart = echarts.init(document.getElementById('chart'))
      const chartData = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: data1
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: data2,
            type: 'bar',
            showBackground: true,
            backgroundStyle: {
              color: 'rgba(180, 180, 180, 0.2)'
            }
          }
        ]
      }
      this.chart.setOption(chartData)
      this.main1 = echarts.init(document.getElementById('main1'))
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: data1
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: data2,
            type: 'line'
          }
        ]
      }
      option && this.main1.setOption(option)

      this.main2 = echarts.init(document.getElementById('main2'))
      const option1 = {
        tooltip: {
          trigger: 'item'
        },
        legend: {
          top: '5%',
          left: 'center'
        },
        series: [
          {
            name: '客户消费占比',
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: 40,
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: false
            },
            data: data3
          }
        ]
      }
      option1 && this.main2.setOption(option1)
    }, 1000)
    },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.afterRefresh]() {
      this.list = this.crud.data
    }
  }
}
</script>

<style scoped>

</style>
